package dbpedia;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.*;
import zclTool.*;
import java.net.*;

public class Instance4DbpClassWithInheritage {

	public static void main2(String[] args) throws Exception{
		String a = URLDecoder.decode("%21%21", "utf-8");
		System.out.println(a);
	}
	/**
	 * @param args
	 */
	static HashMap<String,Integer>rphtitle_id = new HashMap<String,Integer>();
	
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
//		DelimitedReader dr = new DelimitedReader("pagetitles.sorted");
//
//		int ln = 0;
//		while((line = dr.read())!=null){
//			ln++;
//			if(ln % 100000 == 0)System.out.println(ln);
//			int rphid = Integer.parseInt(line[0]);
//			String rphname = line[1];
//			rphtitle_id.put(rphname,rphid);
//		}
//		dr.close();
		
		System.out.println("Start Loading Ontology Tree");
		DP_Ontology_Tree dot =new DP_Ontology_Tree();
		dot.load("dbpedia_3.4.owl");
		DelimitedReader dr = new DelimitedReader("pagetitles.sorted");
		String []line;
		dr = new DelimitedReader("instance4classify");
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("instance4classify.inherit"));

		int ln = 0;
		while((line = dr.read())!=null){
			ln++;
			if(ln % 100000 == 0)System.out.println(ln);
			String title = line[1].replace("_", " ");
			//String new_title = URLDecoder.decode(title, "utf-8");
			String onto = line[0];
			ArrayList<String>anscenters = anscenter(onto,dot);
			for(String a:anscenters){
				if(!a.equals("Thing")){
					osw.write(a+"\t"+title+"\n");
				}
			}
		}
		dr.close();
		osw.close();
		System.out.println(anscenter("Actor",dot));

	}
	public static ArrayList<String> anscenter(String name,DP_Ontology_Tree dot){
		ArrayList<String> result = new ArrayList<String>();
		result.add(name);//add itself first
		String cur_node = name;
		OntologyTreeNode otn = dot.treenodes.get(name);
		
		while(otn!=null && otn.father !=null){
			result.add(otn.fathername);
			otn = otn.father;
		}
		return result;
	}

}
